package aatesting;

public class MySingleton {

	private static MySingleton instance = new MySingleton();
	
	public int counter=0;
	
	// This private constructor is to prevent this object get instantiated more than once.
	private MySingleton(){}
	
	public static MySingleton getInstance()
	{
		return instance;
	}
	
	public void increment()
	{
		this.counter++;
	}
	
	public static void main(String args[])
	{
		MySingleton s = MySingleton.getInstance();		
		s.increment();s.increment();s.increment();
		System.out.println(s.counter); //3
		
		MySingleton s2 = MySingleton.getInstance();
		System.out.println(s2.counter); //3 also, because s2 is the same instance as s, just two different variable pointing to the same instance
		
		String txt;
		txt = (s.equals(s2)) ? "same":"not same";
		System.out.println(txt); //same
	}
}
